const axios = require("axios");
const FormData = require('form-data');
const { transferImages, transferNetworks, transferServers } = require('../transfer/transfer')

const baseUrl = 'http://218.94.117.178:8002/api'

/**
 * 删除镜像
 * @param {*} id  镜像id 
 * @returns 
 */
async function deleteImage(id, token) {
    var config = {
        method: 'delete',
        url: baseUrl + '/image/images/' + id,
        headers: {
            Authorization: 'Bearer ' + token,
        },
    };

    const res = await axios(config)
    const apiData = res.data
    console.log('南京删除镜像接口返回内容：', apiData);
    if (apiData.code !== 0) return apiData;
    return apiData
}

/**\
 * 登录接口
 */
async function login(param) {
    var data = new FormData();
    data.append('username', 'bjtest');
    data.append('password', 'bjtest');
    var config = {
        method: 'post',
        url: baseUrl + '/identity/token',
        headers: {
            ...data.getHeaders()
        },
        data: data
    };

    const res = await axios(config)
    // console.log(JSON.stringify());
    console.log(res.data.access_token);
    return res.data
}

/**
 * 获取镜像接口
 */
async function getImages(token) {
    var config = {
        method: 'get',
        url: baseUrl + '/image/images',
        headers: {
            Authorization: 'Bearer ' + token,
        },
    };

    const res = await axios(config)
    const apiData = res.data
    if (apiData.code !== 0) return { code: apiData.code };
    const images = apiData.data.images;
    return {
        code: apiData.code,
        data: transferImages(images)
    }
}

/**
 * 获取网络管理列表
 */
async function getNetworks(token) {
    var config = {
        method: 'get',
        url: baseUrl + '/network/networks',
        headers: {
            Authorization: 'Bearer ' + token,
        },
    };
    const res = await axios(config)
    const apiData = res.data
    if (apiData.code !== 0) return { code: apiData.code };
    const networks = apiData.data.networks;
    return {
        code: apiData.code,
        data: transferNetworks(networks)
    }
}

/**
 * 查询虚拟机列表
 */
async function getServers(token) {
    var config = {
        method: 'get',
        url: baseUrl + '/compute/servers?details=true',
        headers: {
            Authorization: 'Bearer ' + token,
        },
    };
    const res = await axios(config)
    const apiData = res.data
    if (apiData.code !== 0) return { code: apiData.code };
    const servers = apiData.data.servers;
    return {
        code: apiData.code,
        data: transferServers(servers)
    }
}



/**
 * 生成虚拟机镜像接口
 * @param {*} id 镜像id
 * @param {*} token 
 * @returns 
 */
async function createImage(id, token) {
    console.log('要生成镜像的虚拟机id：', id);
    var config = {
        method: 'post',
        url: baseUrl + '/compute/servers/' + id + '/action',
        headers: {
            Authorization: 'Bearer ' + token,
        },
        data: {
            createImage: {
                name: 'xiezh-'+Date.parse(new Date).toString().slice(9)
            }
        }
    };
    const res = await axios(config)
    const apiData = res.data
    console.log('云计算平台的响应体：', apiData);
    if (apiData.code !== 0) return apiData
    return apiData
}


/**
 * 连接VNC界面
 * @param {*} id 镜像id
 * @param {*} token 
 * @returns 
 */
 async function connectVNC(id, token) {
    console.log('要连接的虚拟机id：', id);
    var config = {
        method: 'post',
        url: baseUrl + '/compute/' + id + '/vnc',
        headers: {
            Authorization: 'Bearer ' + token,
        },
    };
    const res = await axios(config)
    const apiData = res.data
    console.log('云计算平台的响应体：', apiData);
    if (apiData.code !== 0) return apiData
    return apiData
}

module.exports = {
    deleteImage,
    login,
    getImages,
    getServers,
    getNetworks,
    createImage,
    connectVNC,
}
